/**
 * 渲染  表数据  根据不同的表有不同的列  
 * 先将列配置好再渲染数据
 * @param data	表的所有列
 * @param tableId
 */
function render_BSJGL(data,tableId){
//查看表数据时  得先遍历列
var columnsData=[];
var obj;
for ( var i = 0; i < data.length; i++) {
	obj=new Object();
	obj.field=data[i].zdm.toLowerCase();
	obj.title=data[i].zdzs?data[i].zdzs:data[i].zdm;
	obj.formatter=function(row){
		if(row==null)
			return "";
		else
			return row;
	};
	columnsData[i]=obj;
}
	$("#view_3").datagrid({
			columnsTrElement : "<tr ></tr>",
			columnsTdElement : "<td></td>",
			dataTrElement : "<tr></tr>",
			dataTdElement : "<td></td>",
			url:"s/bsjgl/queryByPageAndTable",
			rownumber:true,
			queryParams:{
				table:tableId
			},
			dataHeight:370,
			pagination:true,
			lis:[10,20,30,80],
			columns : columnsData,
			handleColumn:[{
				title:"操作",
				contents:[{
					title:"查看表结构",
					handler:function(lable,row,thisId){
					}
				}]
			},{
				title:"操作2",
				contents:[{
					title:"修改",
					handler:function(lable,row,thisId){
						renderUpdateModal(row,"s/bsjgl/queryForForm","",tableId);
					}
				}]
			}],
			onDblClickRow:function(data){
				alert(JSON.stringify(data));
			}
	});
}

/**
 * 渲染   表结构  视图
 * @param tableId
 */
function render_BJGGL(tableId){
	$("#view_4").datagrid({
			columnsTrElement : "<tr ></tr>",
			columnsTdElement : "<td></td>",
			dataTrElement : "<tr></tr>",
			dataTdElement : "<td></td>",
			url:"s/bjggl/queryByPageAndTable",
			rownumber:true,
			queryParams:{
				tableId:tableId
			},
			dataHeight:370,
			pagination:true,
			lis:[10,20,30,80],
			columns : [ {
				field : "id",
				title : "编号",
				width : "80px"
			},{
				field : "zdm",
				title : "字段名",
				width : "80px"
			},{
				field : "zdlx",
				title : "字段类型",
				width : "80px"
			},{
				field : "zdzs",
				title : "字段注释",
				width : "80px",
				formatter:function(row){
				if(!row)
					return "";
				else
					return row;
				}
			}, {
				field : "zdcd",
				title : "字段长度",
				width : "80px"
			}],
			handleColumn:[{
				title:"操作",
				contents:[{
					title:"查看表数据",
					handler:function(lable,row,thisId){
						$.config["viewRecord"].unshift(thisId);
						$.ajax({
							type:"get",
							async:false,
							url:"s/bjggl/queryById",
							data:{tableId:row.id},
							success:function(result){
								openView(3);
								render_BSJGL(result,row.id);
							}
						});						
					}
				}]
			},{
				title:"操作2",
				contents:[{
					title:"修改",
					handler:function(lable,row,thisId){
						renderUpdateModal(row,"s/bjggl/queryForForm","s/bjggl/doUpdate");
					}
				},{
					title:"删除",
					handler:function(lable,row,thisId){
						
					}
				}]
			}],
			onDblClickRow:function(data){
			}
	});
}

/**
 * 渲染 表集合 视图
 * @param sjzjmc
 */
function render_YSJGL(sjzjmc){
	$("#view_2").datagrid({
		columnsTrElement : "<tr ></tr>",
			columnsTdElement : "<td></td>",
			dataTrElement : "<tr></tr>",
			dataTdElement : "<td></td>",
			url:"s/bxxgl/queryByPageAndFK",
			rownumber:true,
			queryParams:{
				sjzjmc:sjzjmc
			},
			dataHeight:370,
			pagination:true,
			lis:[10,20,30,80],
			columns : [ {
				field : "id",
				title : "编号",
				width : "80px"
			}, {
				field : "bzs",
				title : "表注释",
			}, {
				field : "bm",
				title : "表名",
				width : "80px"
			} , {
				field : "sfszdb",
				title : "是否是字典表",
				width : "80px",
				formatter:function(value){
					if(value==1)
						return "是";
					else
						return "否";
				}
			} ],
			handleColumn:[{
				title:"操作",
				contents:[{
					title:"查看表结构",
					handler:function(lable,row,thisId){
						$.config["viewRecord"].unshift(thisId);
						openView(4);
						render_BJGGL(row.id);
					}
				},{
					title:"查看表数据",
					handler:function(lable,row,thisId){
						$.ajax({
							type:"get",
							async:false,
							url:"s/bjggl/queryById",
							data:{tableId:row.id},
							success:function(result){
								$.config["viewRecord"].unshift(thisId);
								$("#bsjglInsert").data("table",row.id);
								openView(3);
								render_BSJGL(result,row.id);
							}
						});
					}
				}]
			},{
				title:"操作2",
				contents:[{
					title:"修改",
					handler:function(lable,row,thisId){
						renderUpdateModal(row,"s/bxxgl/queryForForm","s/bxxgl/doUpdate");
					}
				},{
					title:"删除",
					handler:function(lable,row,thisId){
						
					}
				}]
			}],
			onDblClickRow:function(data){
			}
	});
}

/**
 * 渲染数据更新模态窗口
 * @param row
 * @param url
 */
function renderUpdateModal(row,url,actionUrl,tableId){
	$("#myModal").modal({backdrop:"static"});
	$("#testForm").myform({
		url:url,
		actionUrl:actionUrl,
		queryParams:{id:row.id,tableId:tableId?tableId:null}
//			fields:[{
//				field:"id",
//				lable:"编号",
//				value:"2",
//				disabled:false
//			},{
//				field:"name",
//				lable:"名字",
//				value:"3",
//				isHide:true
//			},{
//				field:"age",
//				lable:"年龄",
//				value:"4"
//			},{
//				field:"sex",
//				lable:"性别",
//				value:"1"
//			}]
	});
}

/**
 * 执行修改操作
 */
function doUpdateData(){
	var form=$("#testForm").find("form");
	var data=form.serializeArray();
	$.ajax({
		type:"post",
		url:form.attr("action"),
		data:data,
		success:function(result){
			alert(result);
		},
		error:function(xhr){
			alert(xhr.status);
		}
	});
	
	
}

function insertData(type,url,lable){
	var fields;
	$("#myModal").modal({backdrop:"static"});
	$(".modal-header").find("h4").text("新增中");
	if(type==1){
		fields=[{
			field:"sjzjmc",
			lable:"数据子集名称"
		},{
			field:"sjkyhm",
			lable:"数据库用户名"
		}];
	}else if(type==2){
		fields=[{
			field:"bzs",
			lable:"表注释"
		},{
			field:"bm",
			lable:"表名"
		},{
			field:"sfszdb",
			lable:"是否是字典表"
		}];
	}else if(type==4){
		fields=[{
			field:"zdm",
			lable:"字段名"
		},{
			field:"zdlx",
			lable:"字段类型"
		},{
			field:"zdzs",
			lable:"字段注释"
		},{
			field:"zdcd",
			lable:"字段长度"
		}];
	}else if(type==3){
		var tableId=$(lable).data("table");
//		alert(tableId);
		$.ajax({
			type:"get",
			async:false,
			url:"s/bsjgl/insertForForm",
			data:{tableId:tableId},
			success:function(result){
				fields=result;
				var obj=new Object();
				obj.field="tableId";
				obj.lable="表名编号";
				obj.value=tableId;
				fields.push(obj);
//				alert(JSON.stringify(fields));
			}
		});
	}
	$("#testForm").myform({
		fields:fields,
		actionUrl:url
	});
}
